﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trans.member;
using Trans.city;

namespace Trans.depart
{
    public partial class Manage_depart : Form
    {
        public Manage_depart()
        {
            InitializeComponent();
        }

        private void Manage_depart_Load(object sender, EventArgs e)
        {
            this.tb_departTableAdapter.Fill(this.dataSet.tb_depart);


        }

        private void bt_city_Click(object sender, EventArgs e)
        {
            select_city f = new select_city();
           
            if (( f.ShowDialog()==DialogResult.OK)&&
                (f.HasSelected))
            {
                lb_city.Text = f.Name_s;
                DataRowView row = (DataRowView)tbdepartBindingSource.Current;
                row["city_id"] = f.Id;
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            try
            {
                tbdepartBindingSource.EndEdit();
                tb_departTableAdapter.Update(dataSet.tb_depart);
                this.Close();
                MessageBox.Show("操作成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("出错啦" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } 
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            tbdepartBindingSource.CancelEdit();
            this.Close();
        }

        private void bt_master_Click(object sender, EventArgs e)
        {
            Select_master f = new Select_master();
            f.Depart_id = (int)gv_departs.CurrentRow.Cells["c_id"].Value;
            if((f.ShowDialog()==DialogResult.OK)&&
                (f.HasSelected))
            {
                lb_master.Text = f.Name_s;
                DataRowView row = (DataRowView)tbdepartBindingSource.Current;
                row["master_id"] = f.Id;
            }
        }

        private void gv_departs_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataRowView row=(DataRowView)tbdepartBindingSource.Current;
            try
            {
                tb_cityTableAdapter.FillById(dataSet.tb_city, (int)row["city_id"]);
            }catch(Exception ex)
            {
                ex.ToString();
            
            }
            
            try
            {
                tb_memberTableAdapter.FillById(dataSet.tb_member, (int)row["master_id"]);
            }
            catch (System.Exception ex)
            {
                ex.ToString();
            }
            
            
            if (dataSet.tb_city.Rows.Count>0)
                lb_city.Text =(string) dataSet.tb_city.Rows[0]["name"];
            if(dataSet.tb_member.Rows.Count>0)
                lb_master.Text = (string)dataSet.tb_member.Rows[0]["name"];
        }

        private void gv_departs_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            session.Color_Const.set_gv_color(gv_departs);
        }
    }
}
